package main

import (
	"github.com/go-websocket/impl"
	"github.com/gorilla/websocket"
	"net/http"
	"time"
)
var (
	upgrader = websocket.Upgrader{
		//允许跨域
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
	}
)
func wsHandler(w http.ResponseWriter,r *http.Request){
	var (
		wsConn *websocket.Conn
		err error
		conn *impl.Connection
		//msgType int
		data []byte
	)
	if wsConn ,err =upgrader.Upgrade(w,r,nil);err !=nil{
		return
	}
	if conn ,err =impl.InitConnection(wsConn);err !=nil{
		goto ERR
	}
	go func(){
		var(
			err error
		)
		for {
			if err =conn.WriteMessage([]byte("heartbeat"));err !=nil{
				return
			}
			time.Sleep(1*time.Second)
		}

	}()
	for{
		if data, err = conn.ReadMessage() ;err !=nil{
			goto ERR
		}
		if err =conn.WriteMessage(data);err !=nil{
			goto ERR
		}
	}
	//websocket conn
	/*
	for {
		//穿消息体 text
		if _,data, err = conn.ReadMessage() ;err !=nil{
			goto ERR
		}
		if err =conn.WriteMessage(websocket.TextMessage,data);err !=nil{
			goto ERR
		}
	}*/
	ERR:
		wsConn.Close()
	//w.Write([]byte("hello"))
	//upgrader.Upgrade(w,r,nil)


}
func main(){
	http.HandleFunc("/ws",wsHandler)
	http.ListenAndServe("0.0.0.0:7777",nil)
}
